package tools;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 涓�涓畝鍗曠殑鏁版嵁搴撴搷浣滃皝瑁呯被
 */
public class CommTools {

	/**
	 * 将传进来以逗号分隔的字符串转化为List
	 */
	public static List<String> StringToList(String items){
		List<String> list = new ArrayList<String>();
		try{
			while(items.indexOf(",")!=-1){
				int index = items.indexOf(",");
				String sub = items.substring(0,index-1);
				list.add(sub);
				items = items.substring(index+1,items.length());
			}
			if(items.length()!=0){
				list.add(items);
			}
		}catch(Exception e){
			
		}
		return list;
	}
	
    /**
     * 根据传进来的对象 构建where语句
     * @param object 对象
     * @param clazz 待转化类型
     * @return
     */
    public static String buildWhere(Object object, Class clazz,List<String> ignore){
    	String errors = "";
        String returnStr = " Where 1=1 ";
        try{
            Field[] fields = clazz.getDeclaredFields(); 
            for(int i = 0; i < fields.length; i ++){
                Field f = fields[i];
                Object obj = f.get(object);
                if(obj != null){
                	if(ignore == null){
                		String value = obj.toString();
                        returnStr = returnStr + " AND " + f.getName() + " = '" + value + "' ";
                	}else{
                		boolean isIngore = false;
                		for(int j=0;j<ignore.size();j++){
                			if(ignore.get(j).equals(f.getName())){
                				isIngore = true;
                			}
                		}
                		if(!isIngore){
                			String value = obj.toString();
                            returnStr = returnStr + " AND " + f.getName() + " = '" + value + "' ";
                		}
                	}
                    
                }
            }
        }catch (Exception e){
            errors = errors + e.getMessage() + "\n";
        }
        return returnStr;
    }

    /**
     * 创建插入语句
     * @param TableName 表名
     * @param object 转化对象
     * @param clazz 转化类别
     * @return
     */
    public static String buildInsert(String TableName, Object object, Class clazz,List<String> ignore){
        String returnStr = " ";
        returnStr = returnStr + "insert into " + TableName + " ";
        String colums = " ( ";
        String values = " ( ";
        String errors = "";
        try{
            Field[] fields = clazz.getDeclaredFields(); //鏍规嵁 Clss 瀵硅薄鑾峰緱灞炴�э紝绉佹湁鐨勪篃鍙互鑾峰緱
            for(int i = 0; i < fields.length; i ++){
                Field f = fields[i];
                Object obj = f.get(object);
                if(obj != null){
                	if(ignore == null){
                		String value = obj.toString();
                		colums = colums + f.getName() + " , ";
                		values = values + "'" + value + "'" + " , ";
                	}else{
                		boolean isIngore = false;
                		for(int j=0;j<ignore.size();j++){
                			if(ignore.get(j).equals(f.getName())){
                				isIngore = true;
                			}
                		}
                		if(!isIngore){
                			String value = obj.toString();
                    		colums = colums + f.getName() + " , ";
                    		values = values + "'" + value + "'" + " , ";
                		}
                	}
                }
            }
        }catch (Exception e){
            errors = errors + e.getMessage() + "\n";
        }
        colums = colums.substring(0, colums.length() - 2);
        values = values.substring(0, values.length() - 2);

        colums = colums + " ) ";
        values = values + " ) ";
        returnStr += colums + " values " + values + " ; ";
        return returnStr;
    }

    /**
     * 创建更新语句
     * @param TableName 表名
     * @param object 转化对象
     * @param clazz 待转化类别
     * @param where 条件
     * @return
     */
    public static String buildUpdate(String TableName, Object object, Class clazz, String where){
        String returnStr = " ";
        String errors = "";
        returnStr += " Update " + TableName + " set ";
        try{
            Field[] fields = clazz.getDeclaredFields(); //鏍规嵁 Class 瀵硅薄鑾峰緱灞炴�э紝绉佹湁鐨勪篃鍙互鑾峰緱
            for(int i = 0; i < fields.length; i ++){
                Field f = fields[i];
                Object obj = f.get(object);
                if(obj != null){
                    String value = obj.toString();
                    returnStr += f.getName() + " = '" + value + "' , ";
                }
            }
            if(fields.length != 0){
                returnStr = returnStr.substring(0, returnStr.length() - 2);
            }
            if(where != null){
                returnStr += " " + where;
            }
        }catch (Exception e){
            errors += e.getMessage() + "\n";
        }
        return returnStr;
    }



    public static String buildDelete(String TableName,Object object, Class clazz, String where){
    	String returnStr = " ";
        String errors = "";
        returnStr += " delete from " + TableName + " ";
        try{
        	if(where != null){
        		returnStr += " " + where;
        	}
        }catch(Exception e){
        	errors += e.getMessage() + "\n";
        }
    	return returnStr;
    }

}
